﻿using System.ComponentModel;
using System.IO.IsolatedStorage;

namespace Putta.KeyPass
{
    /// <summary>
    /// Represent persistent Application's settings.
    /// </summary>
    public sealed class AppSettings : INotifyPropertyChanging, INotifyPropertyChanged
    {
        /// <summary>
        /// Initialize a new <see cref="AppSettings"/> and load settings from isolated storage.
        /// </summary>
        public AppSettings()
        {
            if (DesignerProperties.IsInDesignTool)
                return;
        }

        /// <inheritdoc/>
        public event PropertyChangedEventHandler PropertyChanged = delegate { };

        /// <inheritdoc/>
        public event PropertyChangingEventHandler PropertyChanging = delegate { };

        private bool SetField<T>(ref T field, T value, string name)
        {
            if (object.Equals(field, value))
                return false;

            IsolatedStorageSettings.ApplicationSettings[name] = value;
            IsolatedStorageSettings.ApplicationSettings.Save();

            PropertyChanging(this, new PropertyChangingEventArgs(name));
            field = value;
            PropertyChanged(this, new PropertyChangedEventArgs(name));

            return true;
        }
    }
}
